package fun.tan90.easy.binlog.manager.model.convert;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import fun.tan90.easy.binlog.manager.dataobject.BinlogOutput;
import fun.tan90.easy.binlog.manager.dict.Dict;
import fun.tan90.easy.binlog.manager.model.cmd.BinlogOutputAddCmd;
import fun.tan90.easy.binlog.manager.model.cmd.BinlogOutputUpdateCmd;
import fun.tan90.easy.binlog.manager.model.vo.BinlogOutputInfoVo;
import fun.tan90.easy.binlog.manager.model.vo.BinlogOutputPageVo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.List;

/**
 * description
 * company 铁人科技
 *
 * @author 李文斌
 * @date 2022/05/05
 */
@Mapper(componentModel = "spring")
public interface BinlogOutputConvert {

    @Mapping(source = "outputParams", target = "outputParams", qualifiedByName = "strToJson")
    @Mapping(source = "outputStatus", target = "outputStatusCn", qualifiedByName = "outputStatusCn")
    @Mapping(source = "outputType", target = "outputTypeCn", qualifiedByName = "outputTypeCn")
    BinlogOutputInfoVo doToInfoVo(BinlogOutput binlogOutput);

    @Mapping(source = "outputParams", target = "outputParams", qualifiedByName = "strToJson")
    @Mapping(source = "outputStatus", target = "outputStatusCn", qualifiedByName = "outputStatusCn")
    @Mapping(source = "outputType", target = "outputTypeCn", qualifiedByName = "outputTypeCn")
    BinlogOutputPageVo doToPageVo(BinlogOutput binlogOutputWay);

    List<BinlogOutputPageVo> dosToPageVos(List<BinlogOutput> binlogOutputWays);

    @Mapping(source = "outputParams", target = "outputParams", qualifiedByName = "jsonToStr")
    BinlogOutput addToDo(BinlogOutputAddCmd addCmd);

    @Mapping(source = "outputParams", target = "outputParams", qualifiedByName = "jsonToStr")
    BinlogOutput updateToDo(BinlogOutputUpdateCmd updateCmd);

    @Named("jsonToStr")
    default String jsonToStr(JSONObject outputParams) {
        return JSONUtil.toJsonStr(outputParams);
    }

    @Named("strToJson")
    default JSONObject strToJson(String outputParams) {
        return JSONUtil.parseObj(outputParams);
    }

    @Named("outputStatusCn")
    default String outputStatusCn(String outputStatus) {
        return Dict.dict("outputStatus", outputStatus);
    }

    @Named("outputTypeCn")
    default String outputTypeCn(String outputType) {
        return Dict.dict("outputType", outputType);
    }
}
